package 每日一题;

public class 替换所有的问号 {
    public String modifyString(String s) {
        char[] chars = s.toCharArray();
        int n = chars.length;
        if (n==1 && chars[0]=='?')
            return "a";
        if (chars[0]=='?'){
            chars[0] = find(' ',chars[1]);
        }
        if (chars[n-1]=='?'){
            chars[n-1] = find(' ',chars[n-2]);
        }
        for (int i = 1; i < chars.length-1; i++) {
            if (chars[i]=='?')
                chars[i] = find(chars[i-1],chars[i+1]);
        }
        return new String(chars);
    }
    //找到一个不和前后字符重复的数
    public char find(char a,char b){
        for (int i = 0; i < 3; i++) {
            char c =(char)('a'+i);
            if (c!=a && c!=b)
                return c;
        }
        return ' ';
    }
}
